package com.qfedu.controller;

import com.qfedu.common.JsonBean;
import com.qfedu.pojo.SysUser;
import com.qfedu.service.SysOrderService;
import com.qfedu.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller//相当于实现Controller接口
@RequestMapping("/order")//相当于在url后加前缀
public class SysOrderController {
    @Autowired//根据类型自动注入对象
    private SysOrderService orderService;

    //查询所有订单
    @RequestMapping("/all.do")
    @ResponseBody//返回前端的是json数据
    public JsonBean selectAll(HttpSession session, int page) {
        JsonBean bean = null;
        SysUser user = (SysUser) session.getAttribute("user");

        try {
            Map<String, Object> map = orderService.selectAllV(user.getId(), page);
            bean = JsonUtils.createJsonBean(1, map);
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(0, e.getMessage());
        }
        return bean;
    }

    //删除订单
    @RequestMapping("")
    @ResponseBody
    public JsonBean delete(Integer id) {
        JsonBean bean = null;
        try {
            orderService.deleteOrder(id);
            bean = JsonUtils.createJsonBean(1, "删除成功");
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(0, "删除失败");
        }
        return bean;
    }

    //提交订单
    @RequestMapping("/add.do")
    @ResponseBody
    public JsonBean addOrder(HttpSession session, Integer cid, Integer getid, Integer backid, String oprice) {

        JsonBean bean = null;
        SysUser user = (SysUser) session.getAttribute("user");
        if (user == null) {
            bean = JsonUtils.createJsonBean(2, "您未登录！");
        } else {
            try {
                orderService.addOrder(user.getId(), cid, getid, backid, oprice);
                bean = JsonUtils.createJsonBean(1, "预定成功！");
            } catch (Exception e) {
                bean = JsonUtils.createJsonBean(0, e.getMessage());
            }
        }
        return bean;
    }
}
